<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Qiu extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 */
	public function init(){
		ini_set("max_execution_time",0);
		set_time_limit(0);
		//$this->output->enable_profiler(TRUE);                // 激活该分析器查看性能信息
	}

	 // ------------双色球--------------------------
	public function index()
	{
		ini_set("max_execution_time",0);
		set_time_limit(0);
		//$this->output->enable_profiler(TRUE);                // 激活该分析器查看性能信息
		$data = $ids = $outs =  array() ;
		$this->load->model('mssq','mqiu');  
		$this->load->model('mresult');  
		$this->load->model('mdlt');  
		$this->load->model('mqlc');  
		//$data['qiu'] = $this->mqiu->get_all();
		//$data['qiu'] = $this->mqiu->get_num('02');

		$number = $this->input->get('number') ;
		$qs = $this->input->get('qs') ;
		if(empty($qs)) 		$qs = $this->input->post('qs') ;
		$data['qs1'] = $qs ;
		$data['title'] = "双色球".$qs."期" ;

		$result = $this->mresult->get_one($qs,1);
		if(empty($result->qiuhao)){
			$qss = $qs - 1 ;
			$result = $this->mresult->get_one($qss,1);
		}
		$result = $this->mresult->get_one($qs,1);
		$qiuhao = @explode('+',$result->qiuhao) ;
		$data['red_result'] = @explode(" ",$qiuhao[0]) ;
		$data['blue_result'] = @explode(" ",$qiuhao[1]) ;
		$data['result'] = $result ;

		$qishu = $this->mqiu->get_qishu();
		$qishu1 = $this->mqlc->get_qishu();
		$qishu2 = $this->mdlt->get_qishu();
		rsort($qishu) ;
		rsort($qishu1) ;
		rsort($qishu2) ;

		$data['ssq_qs'] = $qishu ;  //array('2014126','2014125','2014124' ,'2014123' ,'2014122' ) ;
		$data['dlt_qs'] = $qishu2 ;  //array('14127','14126' ,'14125' ,'14124' ) ;
		$data['qlc_qs'] = $qishu1 ;  //array('2014127','2014126' ,'2014125' ,'2014124' ) ;
		$data['qs'] = $qishu ;  //array('2014127','2014126' ,'2014125' ,'2014124' ) ;

		if(empty($qs)) 		$qs = current($data['qs']) ;

		if(!empty($number)){
			$num_arr = explode(' ',$number) ;
			foreach($num_arr as $k=>$v){
				$v1 = sprintf("%s",$v); 
				$ids[$k] = $this->mqiu->get_ids($qs,$v1);
			}
			$data['number'] = $number ;
			$data['num_arr'] = $num_arr ;

			switch(count($num_arr)){
				case 2:
					$result = array_intersect($ids[0], $ids[1]);
					break;
				case 3:
					$result = array_intersect($ids[0], $ids[1],$ids[2]);
					break;
				case 4:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3]);
					break;
				case 5:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4]);
					break;
				case 6:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5]);
					break;
				case 7:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6]);
					break;
				case 8:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6],$ids[7]);
					break;
				case 9:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6],$ids[7],$ids[8]);
					break;
				case 10:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6],$ids[7],$ids[8],$ids[9]);
					break;
				default:
					$result = array() ;
			}

			foreach($result as $ck=>$cid){
				$outs[$ck] = $this->mqiu->get_one($cid);
			}
			$data['outs'] = $outs ;
		}else{

			// ---------红球-------------------------------
			$red_qiu = array() ;
			$xAxis = $yAxis = '[' ;
			$xAxis1 = $yAxis1 = '[' ;
			//$red_nums = range(1, 35) ;        //  大乐透
			$red_nums = range(1, 33) ;          //   双色球
			foreach($red_nums as $red){
				if($red < 10){
					$red = '0'.$red ;
				}
				$xAxis .= "'".$red."'," ;
				$red_s =  sprintf("%s",$red); 
				$red_qiu[$red] = $this->mqiu->get_num($qs,$red_s);
				$yAxis .= $red_qiu[$red].',' ;
			}
			arsort($red_qiu) ;
			$data['reds'] = $red_qiu ;

		// ------------是否存储---------------
			$red_num = array() ;
			$exist = $this->mresult->exist_id($qs) ;
			if($exist>0){
				$red_qiu0 = array_keys($red_qiu) ;
				foreach($red_qiu0 as $k=>$v){
					if(in_array($v,$data['red_result'])){
						$red_num[] = $k+1 ;
					}
				}
				$out['datas'] = serialize($red_num) ;
				$this->mresult->update($qs,$out) ;
			}else{
				$red_datas = $red_tui = array() ;
				$red_qiu1 = array_keys($red_qiu) ;
				$list = $this->mresult->get_list($qs) ;
				foreach($list as $ls){
					$red_tui1 = $ls->qishu.'期('.$ls->date.') <span style="color:red;font-size:28px;">' ;
					$red_datas = unserialize($ls->datas) ;
					foreach($red_qiu1 as $k=>$v){
						$key = $k+1 ;
						if(@in_array($key,$red_datas)){
							$red_tui1.= $v." " ;
						}
					}
					if($red_datas){
						$red_tui[] = $red_tui1.'</span> ['.@implode(' ',$red_datas).']' ;
					}
				}

				$data['red_tui'] = $red_tui ;
			}

			foreach($red_qiu as $k1=>$v1){
				$xAxis1 .= "'".$k1."'," ;
				$yAxis1 .= $v1.',' ;
			}

			$data['xAxis'] = rtrim($xAxis,',').']';
			$data['yAxis'] = rtrim($yAxis,',').']';
			$data['xAxis1'] = rtrim($xAxis1,',').']';
			$data['yAxis1'] = rtrim($yAxis1,',').']';

			//---------篮球----------------------------------------
			$blue_qiu = array() ;
			$blue_nums = range(1, 16) ;
			foreach($blue_nums as $blue){
				if($blue < 10){
					$blue = '0'.$blue ;
				}
				$blue_s =  sprintf("%s",$blue); 
				$blue_qiu[$blue] = $this->mqiu->get_num($qs,$blue_s,'blue');
			}
			arsort($blue_qiu) ;
			$data['blues'] = $blue_qiu ;
	//echo "<pre>" ;print_r($blue_qiu) ;echo "</pre>" ;

		}

		$this->load->view('qiu',$data);
	}






	public function dlt()
	{
		ini_set("max_execution_time",0);
		set_time_limit(0);
		//$this->output->enable_profiler(TRUE);                // 激活该分析器查看性能信息
		$data = $ids = $outs =  array() ;
		$this->load->model('mdlt','mqiu');  
		$this->load->model('mresult');  
		$this->load->model('mqlc');  
		$this->load->model('mssq');  
		//$data['qiu'] = $this->mqiu->get_all();
		//$data['qiu'] = $this->mqiu->get_num('02');

		$number = $this->input->get('number') ;
		$qs = $this->input->get('qs') ;
		if(empty($qs)) 		$qs = $this->input->post('qs') ;
		$data['qs1'] = $qs ;
		$data['title'] = "大乐透".$qs."期" ;

		$result = $this->mresult->get_one($qs,2);
		if(empty($result->qiuhao)){
			$qss = $qs - 1 ;
			$result = $this->mresult->get_one($qss,2);
		}
		$qiuhao = @explode('+',$result->qiuhao) ;
		$data['red_result'] = @explode(" ",$qiuhao[0]) ;
		$data['blue_result'] = @explode(" ",$qiuhao[1]) ;
		$data['result'] = $result ;

		$qishu = $this->mqiu->get_qishu();
		$qishu1 = $this->mqlc->get_qishu();
		$qishu2 = $this->mssq->get_qishu();
		rsort($qishu) ;
		rsort($qishu1) ;
		rsort($qishu2) ;

		$data['ssq_qs'] = $qishu2 ;  //array('2014126','2014125','2014124' ,'2014123' ,'2014122' ) ;
		$data['dlt_qs'] = $qishu ;  //array('14127','14126' ,'14125' ,'14124' ) ;
		$data['qlc_qs'] = $qishu1 ;  //array('2014127','2014126' ,'2014125' ,'2014124' ) ;
		$data['qs'] = $qishu ;  //array('2014127','2014126' ,'2014125' ,'2014124' ) ;

		if(empty($qs)) 		$qs = current($data['qs']) ;
		if(!empty($number)){
			$num_arr = explode(' ',$number) ;
			foreach($num_arr as $k=>$v){
				$v1 = sprintf("%s",$v); 
				$ids[$k] = $this->mqiu->get_ids($qs,$v1);
			}
			$data['number'] = $number ;
			$data['num_arr'] = $num_arr ;

			switch(count($num_arr)){
				case 2:
					$result = array_intersect($ids[0], $ids[1]);
					break;
				case 3:
					$result = array_intersect($ids[0], $ids[1],$ids[2]);
					break;
				case 4:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3]);
					break;
				case 5:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4]);
					break;
				case 6:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5]);
					break;
				case 7:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6]);
					break;
				case 8:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6],$ids[7]);
					break;
				case 9:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6],$ids[7],$ids[8]);
					break;
				case 10:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6],$ids[7],$ids[8],$ids[9]);
					break;
				default:
					$result = array() ;
			}

			foreach($result as $ck=>$cid){
				$outs[$ck] = $this->mqiu->get_one($cid);
			}
			$data['outs'] = $outs ;
		}else{

			// ---------红球-------------------------------
			$red_qiu = array() ;
			$xAxis = $yAxis = '[' ;
			$xAxis1 = $yAxis1 = '[' ;
			$red_nums = range(1, 35) ;        //  大乐透
			foreach($red_nums as $red){
				if($red < 10){
					$red = '0'.$red ;
				}
				$xAxis .= "'".$red."'," ;
				$red_s =  sprintf("%s",$red); 
				$red_qiu[$red] = $this->mqiu->get_num($qs,$red_s);
				$yAxis .= $red_qiu[$red].',' ;
			}
			arsort($red_qiu) ;
			$data['reds'] = $red_qiu ;

		// ------------是否存储---------------
			$red_num = array() ;
			$exist = $this->mresult->exist_id($qs) ;
			if($exist>0){
				$red_qiu0 = array_keys($red_qiu) ;
				foreach($red_qiu0 as $k=>$v){
					if(in_array($v,$data['red_result'])){
						$red_num[] = $k+1 ;
					}
				}
				$out['datas'] = serialize($red_num) ;
				$this->mresult->update($qs,$out) ;
			}else{
				$red_datas = $red_tui = array() ;
				$red_qiu1 = array_keys($red_qiu) ;
				$list = $this->mresult->get_list($qs) ;
				foreach($list as $ls){
					$red_tui1 = $ls->qishu.'期('.$ls->date.') <span style="color:red;font-size:28px;">' ;
					$red_datas = unserialize($ls->datas) ;
					foreach($red_qiu1 as $k=>$v){
						$key = $k+1 ;
						if(@in_array($key,$red_datas)){
							$red_tui1.= $v." " ;
						}
					}
					if($red_datas){
						$red_tui[] = $red_tui1.'</span> ['.@implode(' ',$red_datas).']' ;
					}
				}

				$data['red_tui'] = $red_tui ;
			}

			foreach($red_qiu as $k1=>$v1){
				$xAxis1 .= "'".$k1."'," ;
				$yAxis1 .= $v1.',' ;
			}

			$data['xAxis'] = rtrim($xAxis,',').']';
			$data['yAxis'] = rtrim($yAxis,',').']';
			$data['xAxis1'] = rtrim($xAxis1,',').']';
			$data['yAxis1'] = rtrim($yAxis1,',').']';

			//---------篮球----------------------------------------
			$blue_qiu = array() ;
			$blue_nums = range(1, 12) ;
			foreach($blue_nums as $blue){
				if($blue < 10){
					$blue = '0'.$blue ;
				}
				$blue_s =  sprintf("%s",$blue); 
				$blue_qiu[$blue] = $this->mqiu->get_num($qs,$blue_s,'blue');
			}
			arsort($blue_qiu) ;
			$data['blues'] = $blue_qiu ;
	//echo "<pre>" ;print_r($blue_qiu) ;echo "</pre>" ;

		}

		// 杀号
		$data['red_exclude_result'] = array(14,03,31,07,27,16,16,26,29,27) ;
		$data['blue_exclude_result'] = array(12,'08',10,05,10,04,02,12,'08',06) ;

		$this->load->view('qiu',$data);
	}






	 // ------------七乐彩--------------------------
	public function qlc()
	{
		$data = $ids = $outs =  array() ;
		$this->load->model('mqlc','mqiu');  
		$this->load->model('mresult');  
		$this->load->model('mdlt');  
		$this->load->model('mssq');  

		$number = $this->input->get('number') ;
		$qs = $this->input->get('qs') ;
		if(empty($qs)) 		$qs = $this->input->post('qs') ;
		$data['qs1'] = $qs ;
		$data['title'] = "七乐彩".$qs."期" ;

		$result = $this->mresult->get_one($qs,3);
		if(empty($result->qiuhao)){
			$qss = $qs - 1 ;
			$result = $this->mresult->get_one($qss,3);
		}
		$result = $this->mresult->get_one($qs,3);
		$qiuhao = @explode('+',$result->qiuhao) ;
		$data['red_result'] = @explode(" ",$qiuhao[0]) ;
		$data['blue_result'] = @explode(" ",$qiuhao[1]) ;
		$data['result'] = $result ;

		$qishu = $this->mqiu->get_qishu();
		$qishu1 = $this->mdlt->get_qishu();
		$qishu2 = $this->mssq->get_qishu();
		rsort($qishu) ;
		rsort($qishu1) ;
		rsort($qishu2) ;

		$data['ssq_qs'] = $qishu2 ;  //array('2014126','2014125','2014124' ,'2014123' ,'2014122' ) ;
		$data['dlt_qs'] = $qishu1 ;  //array('14127','14126' ,'14125' ,'14124' ) ;
		$data['qlc_qs'] = $qishu ;  //array('2014127','2014126' ,'2014125' ,'2014124' ) ;
		$data['qs'] = $qishu ;  //array('2014127','2014126' ,'2014125' ,'2014124' ) ;

		if(empty($qs)) 		$qs = current($data['qs']) ;
		if(!empty($number)){
			$num_arr = explode(' ',$number) ;
			foreach($num_arr as $k=>$v){
				$v1 = sprintf("%s",$v); 
				$ids[$k] = $this->mqiu->get_ids($qs,$v1);
			}
			$data['number'] = $number ;
			$data['num_arr'] = $num_arr ;

			switch(count($num_arr)){
				case 2:
					$result = array_intersect($ids[0], $ids[1]);
					break;
				case 3:
					$result = array_intersect($ids[0], $ids[1],$ids[2]);
					break;
				case 4:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3]);
					break;
				case 5:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4]);
					break;
				case 6:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5]);
					break;
				case 7:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6]);
					break;
				case 8:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6],$ids[7]);
					break;
				case 9:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6],$ids[7],$ids[8]);
					break;
				case 10:
					$result = array_intersect($ids[0], $ids[1],$ids[2],$ids[3],$ids[4],$ids[5],$ids[6],$ids[7],$ids[8],$ids[9]);
					break;
				default:
					$result = array() ;
			}

			foreach($result as $ck=>$cid){
				$outs[$ck] = $this->mqiu->get_one($cid);
			}
			$data['outs'] = $outs ;
		}else{

			// ---------红球-------------------------------
			$red_qiu = array() ;
			$xAxis = $yAxis = '[' ;
			$xAxis1 = $yAxis1 = '[' ;
			//$red_nums = range(1, 35) ;        //  大乐透
			$red_nums = range(1, 30) ;          //   双色球
			foreach($red_nums as $red){
				if($red < 10){
					$red = '0'.$red ;
				}
				$xAxis .= "'".$red."'," ;
				$red_s =  sprintf("%s",$red); 
				$red_qiu[$red] = $this->mqiu->get_num($qs,$red_s);
				$yAxis .= $red_qiu[$red].',' ;
			}
			arsort($red_qiu) ;
			$data['reds'] = $red_qiu ;

		// ------------是否存储---------------
			$red_num = array() ;
			$exist = $this->mresult->exist_id($qs) ;
			if($exist>0){
				$red_qiu0 = array_keys($red_qiu) ;
				foreach($red_qiu0 as $k=>$v){
					if(in_array($v,$data['red_result'])){
						$red_num[] = $k+1 ;
					}
				}
				$out['datas'] = @serialize($red_num) ;
				$this->mresult->update($qs,$out) ;
			}else{
				$red_datas = $red_tui = array() ;
				$red_qiu1 = array_keys($red_qiu) ;
				$list = $this->mresult->get_list($qs) ;
				foreach($list as $ls){
					$red_tui1 = $ls->qishu.'期('.$ls->date.') <span style="color:red;font-size:28px;">' ;
					$red_datas = unserialize($ls->datas) ;
					foreach($red_qiu1 as $k=>$v){
						$key = $k+1 ;
						if(@in_array($key,$red_datas)){
							$red_tui1.= $v." " ;
						}
					}
					if($red_datas){
						$red_tui[] = $red_tui1.'</span> ['.@implode(' ',$red_datas).']' ;
					}
				}

				$data['red_tui'] = $red_tui ;
			}


			foreach($red_qiu as $k1=>$v1){
				$xAxis1 .= "'".$k1."'," ;
				$yAxis1 .= $v1.',' ;
			}

			$data['xAxis'] = rtrim($xAxis,',').']';
			$data['yAxis'] = rtrim($yAxis,',').']';
			$data['xAxis1'] = rtrim($xAxis1,',').']';
			$data['yAxis1'] = rtrim($yAxis1,',').']';

			//---------篮球----------------------------------------
			/*
			$blue_qiu = array() ;
			$blue_nums = range(1, 12) ;
			foreach($blue_nums as $blue){
				if($blue < 10){
					$blue = '0'.$blue ;
				}
				$blue_s =  sprintf("%s",$blue); 
				$blue_qiu[$blue] = $this->mqiu->get_num($qs,$blue_s,'blue');
			}
			arsort($blue_qiu) ;
			$data['blues'] = $blue_qiu ;
			*/
	//echo "<pre>" ;print_r($blue_qiu) ;echo "</pre>" ;

		}

		$this->load->view('qiu',$data);
	}

	public function test(){
		$red = '03 05 08 09 16 17 29' ;
		$blue = '08 11 22' ;
		
		$red_arr = explode(' ',$red) ;
		$red_count = count($red_arr) ;
		$red_scale = ($red_count-1)*($red_count-2)*($red_count-3)*($red_count-4)/24 ;

		$blue_arr = explode(' ',$blue) ;
		$blue_count = count($blue_arr) ;
		$blue_scale = ($blue_count)*($blue_count-1)/2 ;

		$num = $red_scale*$blue_scale ;
		echo "<pre>" ;print_r($num) ;echo "</pre>" ;
	}

	public function test1($ns='03'){
		//$red = '(03) 05 08 09 16 17' ;
		$red = '10 17 30 05 06 07 09' ;
		$blue = '05' ;
		
		$num = $this->get_num($ns='03',$red,$blue) ;
		echo "<pre>" ;print_r($num) ;echo "</pre>" ;

	}

	// ---------获得彩票注数--------------------
	public function get_num($ns='03',$red,$blue){
		//  ---------篮球注数-----------------------------
		$blue_arr = explode(' ',$blue) ;
		$blue_count = count($blue_arr) ;
		$blue_scale = $blue_count ;


		//  ---------红球注数-----------------------------
		if(strpos($red,')')=== false){
			$red_arr = explode(' ',$red) ;
			$red_count = count($red_arr) ;
			$red_scale = $red_count*($red_count-1)*($red_count-2)*($red_count-3)*($red_count-4)*($red_count-5)/720 ;
		}else{
			$dan = $ns_key = 0 ;
			$red = str_replace(')',') ',$red) ;
			$red_arr = explode(' ',$red) ;
			foreach($red_arr as $ks=>$vs){
				if(strpos($vs,')')!==false){
					$dan = $ks+1;
				}
				if(strpos($vs,$ns)!==false){
					$ns_key = $ks+1 ;
				}
			}

			$yuma = count($red_arr) - $dan ;       //  剩余球，余码
			if($ns_key <= $dan){    // 是胆码
				$tuo = 6 - $dan ;
				$bottom = $top = 1 ;
				for($i=0;$i<$tuo;$i++){
					$bottom = $bottom*($yuma-$i) ;
					$top = $top*($tuo-$i) ;
				}
				$red_scale = $bottom / $top ;
			}else{
				$tuo = 6 - $dan - 1 ;
				$bottom = $top = 1 ;
				for($i=0;$i<$tuo;$i++){
					$bottom = $bottom*(($yuma-1)-$i) ;
					$top = $top*($tuo-$i) ;
				}
				$red_scale = $bottom / $top ;
			}
		}
		$num = $red_scale*$blue_scale ;
		return $num ;
	}


}